function Files(){
	var self = this;
	
	self.createDir = function(el){
		
		var name = prompt("Zadejte jméno adresáře: ");
		var link = el.href;
		var id_parent = $("#id_parent").val();
		
		if(name){
		    $.getJSON(link, {'name': name, 'id_parent' : id_parent}, function(payload) {
		    		system.spinner();
		    		self.refreshDir(payload.files);
		    });
		}
		
		return false;
	};
	
	self.deleteDir = function(el, id_file){
		
		if(confirm("Opravdu smazat položku?")){
			var id_parent = $("#id_parent").val();
			var link = el.href;
		    $.getJSON(link, {'id_file': id_file, 'id_parent' : id_parent}, function(payload) {
		    		system.spinner();
		    		self.refreshDir(payload.files);
		    });
		    return false;
		}else{
			
		return false;
		}
	};
	
	self.changeDirectory = function(el, id_file){
		var link = el.href;
	    $.getJSON(link, {'id_file': id_file}, function(payload) {
	
	    		system.spinner();
	    		self.refreshDir(payload.files);
	    		system.refreshSnippets(payload);
	    });
	};
	
	self.refreshDir = function(files){
		
		$("#snippet--files").html(files);
		// zmenime adresar pro upload pres flash
		$('#uploadify').uploadifySettings('folder',base_uri + 'upload/' + $("#dir_path").val());
	};
	
	self.saveFiles = function(files){
		var id_parent = $("#id_parent").val();
		var link = base_address + "admin/system/view-files/?do=saveFiles";
		$.post(link, {'files': files, "id_parent" : id_parent}, function(payload) {
	    	system.spinner();
	    	self.refreshDir(payload.files);
	    });
	    return false;
	};
	
	$("#add").live("change", function(){

		if($("#add:checked").val())	$("#addFile").show("slow");
		else $("#addFile").hide("slow");
	});
}

var files = new Files();

$(document).ready(function() {
		var files_ = new Array();
		var citac = 0;
		$("#uploadify").uploadify({
			'uploader'       : base_address + 'libs/uploadify/uploadify.swf',
			'script'         : base_address + 'libs/uploadify/uploadify.php',
			'cancelImg'      : base_address + 'libs/upload/cancel.png',
			'folder'         : base_uri + '/upload',
			'queueID'        : 'fileQueue',
			'buttonText'	 : 'Vybrat soubory',
			'auto'           : false,
			'multi'          : true,
			onComplete: function(event, queueID, fileObj, response, data) {
				files_[citac] = fileObj;
				citac++;
				},
			onAllComplete: function(){
					tmp = files_;
					files_ = new Array();
					citac = 0;
					files.saveFiles(tmp);
				},
			onProgress: function(){
					system.spinner();
				}
		});

});